<?php
    session_start();
    require_once('userlevel.php'); //Require/include the permissions file
    require_once('template.php'); //Require/include the template file
    require_once('functions.php'); //Require/include the functions file
    require_once('db.php'); //Require/include the file with the database connection information
     
    if (!$_SESSION['user_loggedin']) {
        die ("Not logged in... <script>document.location.href='login.php'</script>");
    } elseif (!($_SESSION['userlevel'] >= $userlevel['stoptrackingfile'])) {
        die (errorpage ("Your user level is not high enough to use this feature!", 'Stop tracking all files in a directory'));
    } else {
        $dir = strip_tags(mysql_escape_string($_GET['dir']));
        //Connect DB using db.php
        $db = new my_db;
        template_headtag("Stop tracking all files in a directory");
        template_header();
        template_left();
        if (!empty($dir)) {
            echo "<div class='pageheadertext'>Stop tracking all files in directory:</div>";
            echo "<div class='subheadertext'>$dir</div><br/>";
             
             
            if ($_POST['submitted']) {
                //Connect to DB and connect to the database using db.php
                $db = new my_db;
                $db2 = new my_db;
                if ($_POST['subdirectories']) {
                    $db->query("SELECT * FROM trackedfiles WHERE filepath LIKE '$dir%'");
                    while ($db->next_record()) {
                        $filepath = $db->f("filepath");
                        $filename = $db->f("filename");
                        $lastrevdate = date("Y-m-d h:i:s"); //get the current date/time
                        $lastuser = $_SESSION['username'];
                        $db2->query("INSERT INTO history (filename, filepath, revhash, revdate, user, revsummary) VALUES('$filename','$filepath','00000000000000000000000000000000','$lastrevdate', '$lastuser','Tracking stopped') ");
                    }
                    $rowcount = $db->nf();
                    $db->query("DELETE FROM trackedfiles WHERE filepath LIKE '$dir%'");
                } else {
                    $db->query("SELECT * FROM trackedfiles WHERE filepath='$dir'");
                    while ($db->next_record()) {
                        $filepath = $db->f("filepath");
                        $filename = $db->f("filename");
                        $filehash = $db->f("filehash");
                        $lastrevdate = date("Y-m-d h:i:s"); //get the current date/time
                        $lastuser = $_SESSION['username'];
                        $db2->query("INSERT INTO history (filename, filepath, revhash, revdate, user, revsummary) VALUES('$filename','$filepath','00000000000000000000000000000000','$lastrevdate', '$lastuser','Tracking stopped') ");
                    }
                    $rowcount = $db->nf();
                    $db->query("DELETE FROM trackedfiles WHERE filepath='$dir'");
                }
                if ($rowcount == 1) {
                    echo "<div align='center'>Tracking stopped on $rowcount file<br/><a href='main.php'>Back</a></div>";
                } else {
                    echo "<div align='center'>Tracking stopped on $rowcount files<br/><a href='main.php'>Back</a></div>";
                }
            } else {
                $PageData[title] = "Stop Tracking a directory";
                $dir = strip_tags($_GET['dir']);
                 
                echo " <div class='subheadertext2'>Are you sure you want to stop tracking all files in this directory?</div>";
                echo "
                    <form method='post' action='stoptrackingdir.php?dir=$dir'>
                    <div align='center'>
                    Also stop tracking all files in subdirectories? <input type='radio' name='subdirectories' value='TRUE'/>Yes &nbsp;&nbsp;&nbsp;<input type='radio' name='subdirectories' value='FALSE' checked='checked'/>No <br/>
                    <input type='hidden' name='submitted' value='TRUE'/>
                    <input type='submit' value='Stop tracking'/>
                    </div></form>
                    ";
            }
        } else {
            errobox("Invalid directory");
        }
        template_footer();
         
    }
?>
